package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.entity.Direction;
import cn.edu.cimv.dao.entity.Option;
import cn.edu.cimv.pojo.Message;
import cn.edu.cimv.service.OptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 门铎 on 2016/11/24.
 */
@Controller
@RequestMapping("/cimv/option")
public class OptionController {

    @Autowired
    private Message message;
    @Autowired private OptionService optionService;
    @RequestMapping(value = "direction/list",method = RequestMethod.GET)
    public String direction(Model model){
        model.addAttribute("options",optionService.queryTitles("direction"));
        return "option/direction";
    }
    @RequestMapping(value = "direction/new",method = RequestMethod.GET)
    public String directionNew(Model model){
        return "option/direction-new";
    }
    @RequestMapping(value = "direction/update/{id}",method = RequestMethod.GET)
    public  String update(Model model, @PathVariable("id")int id){
        Option option = optionService.queryTitlesById(id);
        model.addAttribute("option",option);

        return "option/direction-update";
    }
    @RequestMapping(value = "direction/contentList",method = RequestMethod.GET)
    public String contentList(Model model){
        model.addAttribute("directiona",optionService.queryDirections());
        return "option/content-list";
    }
    @RequestMapping(value = "direction/contentNew",method = RequestMethod.GET)
    public String contentNew(Model model){
        model.addAttribute("options",optionService.queryTitles("direction"));
        return "option/content-new";
    }
    @RequestMapping(value = "direction/contentUpdate/{id}",method = RequestMethod.GET)
    public  String contentUpdate(Model model, @PathVariable("id")int id){
        Direction direction = optionService.queryDirectionsById(id);
        model.addAttribute("direction",direction);
        model.addAttribute("options",optionService.queryTitles("direction"));

        return "option/content-update";
    }

    @RequestMapping(value = "direction/contentUpdateD",method = RequestMethod.POST)
    public @ResponseBody Message contentUpdateD(Direction direction){
        optionService.updateDirection(direction);
        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "direction/create",method = RequestMethod.POST)
    public @ResponseBody Message create(Model model, Option option){
        option.setCataType("direction");
        optionService.insertOption(option);
        message.setMsg(InfoConstant.CREATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "direction/contentCreate",method = RequestMethod.POST)
    public @ResponseBody Message contentCreate(Model model, Direction direction){
        optionService.insertDirection(direction);
        message.setMsg(InfoConstant.CREATE_NORMAL_SUCCESS);
        return message;
    }

    @RequestMapping(value = "direction/updateDirection",method = RequestMethod.POST)
    public @ResponseBody Message updateDirection(Model model, Option option){
        optionService.updateOption(option);
        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "direction/delete/{id}",method = RequestMethod.GET)
    public @ResponseBody Message delete(Model model,@PathVariable("id")int id){
        optionService.deleteOption(id);
        message.setMsg(InfoConstant.DELETE_NORMAL_SUCCESS);
        return message;
    }

    @RequestMapping(value = "direction/contentDelete/{id}",method = RequestMethod.GET)
    public @ResponseBody Message contentDelete(Model model,@PathVariable("id")int id){
        optionService.deleteDirection(id);
        message.setMsg(InfoConstant.DELETE_NORMAL_SUCCESS);
        return message;
    }

}
